﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.ScrumTool.Client.ControlService;
using UIT.ScrumTool.Client.Utilities.Team;

namespace UIT.ScrumTool.Client.Controller
{
    public class ProjectUserCtrl
    {
        #region Thuoc Tinh
        //lay kenh ProjectUser
        private static ControlService.ProjectUserServiceClient projectUserServiceClient = new ControlService.ProjectUserServiceClient();
        #endregion

        public static List<ProjectUserDto> ProjectUserGetByAll()
        {
            try
            {
                return projectUserServiceClient.ProjectUserGetByAll().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static ProjectUserDto ProjectUserGetById(int id)
        {
            try
            {
                return projectUserServiceClient.ProjectUserGetById(id);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static List<ProjectUserDto> ProjectUserGetByProjectId(int projectId)
        {
            try
            {
                return projectUserServiceClient.ProjectUserGetByProjectId(projectId).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static List<ProjectUserDto> ProjectUserGetByProjectIdNoTeam(int projectId)
        {
            try
            {
                return projectUserServiceClient.ProjectUserGetByProjectIdNoTeam(projectId).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static List<ProjectUserDto> ProjectUserGetByTeamId(int teamId)
        {
            try
            {
                return projectUserServiceClient.ProjectUserGetByTeamId(teamId).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static int ProjectUserInsert(ProjectUserDto projectUser)
        {
            try
            {
                return projectUserServiceClient.ProjectUserInsert(projectUser);
            }
            catch (Exception)
            {
                return -1;
            }
        }

        public static bool ProjectUserUpdate(ProjectUserDto projectUserDto)
        {
            try
            {
                return projectUserServiceClient.ProjectUserUpdate(projectUserDto);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ProjectUserUpdateRole(int id, int userRole)
        {
            try
            {
                return projectUserServiceClient.ProjectUserUpdateRole(id, userRole);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ProjectUserDelete(int id)
        {
            try
            {
                return projectUserServiceClient.ProjectUserDelete(id);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ProjectUserDeleteByProjectId(int projectId)
        {
            try
            {
                return projectUserServiceClient.ProjectUserDeleteByProjectId(projectId);
            }
            catch (Exception)
            {
                return false;
            }
        }

        // Get Member Info List of a Project (ProjectId)
        public static List<MemberInfo> GetProjectMember(int projectId)
        {
            List<MemberInfo> lstMember = new List<MemberInfo>();
            MemberInfo memberInfo = new MemberInfo();
            List<ProjectUserDto> lstProjectUser = ProjectUserGetByProjectId(projectId);
            if (lstProjectUser != null)
            {
                foreach (var projectUser in lstProjectUser)
                {
                    UserDto userDto = UserCtrl.UserGetById(projectUser.UserId);
                    memberInfo = MemberInfo.CreateMemberInfo(userDto, projectUser.ProjectId, projectUser.UserRole, projectUser.Id, projectUser.TeamId);
                    lstMember.Add(memberInfo);
                }
            }
            return lstMember;
        }

        // Get Member Info List don't have Team of a Project
        public static List<MemberInfo> GetProjectMemberNoTeam(int projectId)
        {
            List<MemberInfo> lstMember = new List<MemberInfo>();
            MemberInfo memberInfo = new MemberInfo();
            List<ProjectUserDto> lstProjectUser = ProjectUserGetByProjectIdNoTeam(projectId);
            if (lstProjectUser != null)
            {
                foreach (var projectUser in lstProjectUser)
                {
                    UserDto userDto = UserCtrl.UserGetById(projectUser.UserId);
                    memberInfo = MemberInfo.CreateMemberInfo(userDto, projectUser.ProjectId, projectUser.UserRole, projectUser.Id, projectUser.TeamId);
                    lstMember.Add(memberInfo);
                }
            }
            return lstMember;
        }
    }
}
